<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addpermission(1,0)">添加权限</el-button>
        </div>
        <el-table border :data="permission" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button size="small" type="text" @click="addpermission(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="looklist(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="dellistpermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <PermissList ref="reference" :show-dialog.sync="showDialog" />
    </div>
  </div>
</template>

<script>
import { permissionListAPI, DelpermissionAPI, LookListAPI } from '@/api/permission'
import { truesListData } from '@/utils/index'
import PermissList from '@/views/permission/permisslist/index.vue'
export default {
  name: 'Permission',
  components: {
    PermissList
  },
  data() {
    return {
      permission: [],
      showDialog: false
    }
  },
  created() {
    this.permissionList()
  },
  methods: {
    /**
     * 获取权限列表
     */
    async permissionList() {
      const { data: res } = await permissionListAPI()
      // console.log(res)
      this.permission = truesListData(res, '0')
      // console.log(this.permission)
    },
    /**
     * 删除权限点
     */
    async dellistpermission(id) {
      // console.log(id)
      this.$confirm('确认删除吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async() => {
        await DelpermissionAPI(id)
        this.$message.success('删除成功')
        this.permissionList()
      })
    },
    /**
     * 查看权限点
     */
    async  looklist(id) {
      this.showDialog = true
      const { data: res } = await LookListAPI(id)
      console.log(res)
      // console.log(this.$refs.reference)
      this.$refs.reference.formData = res
    },
    addpermission(type, pid) {
      this.showDialog = true
      this.$refs.reference.opendata(type, pid)
    }
  }
}
</script>
